# -*-encoding: utf-8 -*-

import threading
import EmailInitial
import smtplib
from email.mime.text import MIMEText

class EmailThread(threading.Thread):
    
    #receiver = None
    #string = ""

    def __init__(self, receiver, string):
        threading.Thread.__init__(self)
        self.receiver = receiver
        self.string = string

    def run(self):
        try:
           # if EmailInitial.smtpObj:
           #     pass
           # else:
           #     try:
           #         EmailInitial.smtpObj = smtplib.SMTP_SSL(EmailInitial.smtp_server, 465)
           #         print "[EMAIL][SMTP][OK]"
           #     except Exception, e1:
           #         print "[EMAIL][SMTP][ERROR] - ", e1

            try:
                smtpObj = smtplib.SMTP_SSL(EmailInitial.smtp_server, 465)
                print "[EMAIL][SMTP][OK]"
            except Exception, e1:
                print "[EMAIL][SMTP][ERROR] - ", e1

            msg = MIMEText(self.string, _subtype='plain',_charset='utf8')
            msg['Subject'] = "来自《多宝串》的测试邮件"
            msg['From'] = EmailInitial.sender
            smtpObj.login(EmailInitial.sender, EmailInitial.password)
            smtpObj.sendmail(EmailInitial.sender, self.receiver, msg.as_string())
            smtpObj.close()
            print "[EMAIL][SEND][OK]"
        except Exception, e:
            print "[EMAIL][SEND][ERROR] - ", e
